package com.monitorRoutine.monitorRoutineServer.service.impl;

import com.alibaba.fastjson.JSON;
import com.monitorRoutine.monitorRoutineServer.entity.ControlCommand;
import com.monitorRoutine.monitorRoutineServer.entity.ExcelTempla;
import com.monitorRoutine.monitorRoutineServer.service.ExcelService;
import com.monitorRoutine.monitorRoutineServer.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ExcelServiceImpl implements ExcelService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Value("${file.savePath}")
    String SAVE_PATH;
    @Override
    public void scanTemplate(String filename) throws Exception {
        String  filePath=SAVE_PATH+filename;
        File file = new File(filePath);
        if (!file.exists()) {
            throw new Exception("没有文件不存在");
        }
        String name = file.getName();

        try (FileInputStream fis = new FileInputStream(filePath)) {
            // 使用WorkbookFactory创建XSSFWorkbook实例
            Workbook workbook = WorkbookFactory.create(fis);

            List<Object> results = redisTemplate.execute(new SessionCallback<List<Object>>() {

                @Override
                public List<Object> execute(RedisOperations operations) throws DataAccessException {
                    // 开始管道
                    operations.multi();
                    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                        Sheet sheet = workbook.getSheetAt(i);
                        for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {
                            Row row = sheet.getRow(j);
                            int totalCellCount = row.getLastCellNum() + 1;
                            for (int k = 0; k < row.getLastCellNum() + 1; k++) {
                                Cell cell = row.getCell(k);
                                ExcelTempla validJson = JSONUtil.isValidJson(cell.getStringCellValue(), ExcelTempla.class);
                                if (validJson == null) {
                                    continue;
                                }
                                //生成id
                                String id = i + "_" + j + "_" + k;
                                validJson.setId(id);
                                redisTemplate.opsForList().rightPush(name, JSON.toJSONString(validJson));
                            }
                        }


                    }
                    // 执行管道中的所有命令
                    return operations.exec();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
